//
//  CZwelcomeViewController.swift
//  crkWeibo
//
//  Created by 陈荣科 on 15/12/19.
//  Copyright © 2015年 crk. All rights reserved.
//

import UIKit
import SDWebImage

class CZWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //准备UI
        prepareUI()
        
        //显示上一次保存的头像
        setIconVeiw()
        
        //加载用户的信息
        CZUserAccount.userAccount?.loadUserInfo({ (error) -> () in
            
            if error != nil{
            
                print("控制器，获取用户数据错误:\(error)")
                return
            }
            
            //加载头像
            self.setIconVeiw()
        })
    }

    func setIconVeiw() {

        //没有错误，加载头像
        if let avatar_large = CZUserAccount.userAccount?.avatar_large{
            
            let iconURL = NSURL(string: avatar_large)
            print("iconURL:\(iconURL)")
            
            //显示当前用户头像
            self.iconView.sd_setImageWithURL(iconURL, placeholderImage: UIImage(named: "avatar_default_big"))
        }

    }
    
    private var iconViewBottomCon: NSLayoutConstraint?
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        iconViewBottomCon?.constant = -(UIScreen.mainScreen().bounds.height-160)
        
        UIView.animateWithDuration(1.5, delay:0
            , usingSpringWithDamping: 0.5, initialSpringVelocity: 2, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                //刷新
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //将label渐变出来
                self.nameLabel.alpha = 0
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.nameLabel.alpha = 1.0
                    self.nameLabel.hidden = false
                    }, completion: { (_) -> Void in
                        //_表示占位，对这个参数不关心
                        print("所有动画完成")
                        
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
                })
        }
    }
    
    ///准备UI
    func prepareUI() {
    
    //添加子控件
        view.addSubview(backgroundImage)
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
    //设置约束
        
        //关闭Autoresizing
//        backgroundImage.translatesAutoresizingMaskIntoConstraints = false
//        
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        
//        nameLabel.translatesAutoresizingMaskIntoConstraints = false

        //设置backgroundImage的约束
        
        backgroundImage.ff_Fill(view)
//        //上
//       view.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        //下
//        view.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//        //左
//        view.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        
//        //右
//        view.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        //设置iconview的约束
        
       let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        
        // 头像,水平和父控件重合
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        // 垂直方向头像的底部和父控件的底部重合
//        iconViewBottomCon = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        
//        view.addConstraint(iconViewBottomCon!)
        
        //取出头像iconView底部约束赋值给iconViewButtonCon属性
        //去哪个View上面的约束，就哪view来调用
        iconViewBottomCon = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
//        
//        //头像的高度
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        //头像的高度
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        //设置nameLabel约束
        
        nameLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
        
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
//        
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        
//        
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    }
    
    
    //MARK: - 懒加载
    
    ///背景图片
    private lazy var backgroundImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    ///头像图片
    private lazy var iconView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imageView.layer.cornerRadius = 42.5
        
        imageView.layer.masksToBounds = true
        
        return imageView
    }()


    
    ///用户名称
    private lazy var nameLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(14)
        
        label.textAlignment = NSTextAlignment.Center
        
        label.hidden = true
        
        return label
    }()
    


}
